class Deposito < ActiveRecord::Base
  has_many :transportes, :dependent => :destroy
  has_many :fatantdepos, :dependent => :destroy
  
  
  def soma_s(status)
    @soma_s = Transporte.sum :pares, :conditions => ["status_id =?",status]
    @soma_s == nil ? 0 : @soma_s
  end
    
  def soma_s_d(status,deposito)
    @soma_s_d = Transporte.sum :pares, :conditions => ["deposito_id =? AND status_id =?",deposito,status]
    @soma_s_d == nil ? 0 : @soma_s_d
  end
  
  def soma_s_data(status,data)
    @soma_s_data = Transporte.sum :pares, :conditions => ["created_at LIKE ? AND status_id =?","#{data}%",status]
    @soma_s_data == nil ? 0 : @soma_s_data
  end

  def soma_d(deposito)
    @soma_d = Transporte.sum :pares, :conditions => ["deposito_id =?",deposito]
  end

  def soma_d_dt(deposito,data)
    @soma_s_data = Transporte.sum :pares, :conditions => ["created_at LIKE ? AND deposito_id =?","#{data}%",deposito]
  end
  
  def soma_dt(data)
    @soma_s_data = Transporte.sum :pares, :conditions => ["created_at LIKE ?","#{data}%"]
  end

  def soma_s_d_dt(status,deposito,data)
    @soma_s_data = Transporte.sum :pares, :conditions => ["created_at LIKE ? AND deposito_id =? AND status_id =?","#{data}%",deposito,status]
  end

  def soma_s_d_dt_f(status,deposito,data)
    @soma_s_data = Transporte.sum :pares, :conditions => ["ended_at LIKE ? AND deposito_id =? AND status_id =?","#{data}%",deposito,status]
    @soma_s_data == nil ? 0 : @soma_s_data
  end 

  def soma_s_dt_f(status,data)
    @soma_s_data = Transporte.sum :pares, :conditions => ["ended_at LIKE ? AND status_id =?","#{data}%",status]
    @soma_s_data == nil ? 0 : @soma_s_data
  end    
  
  def soma_disponivel_d(deposito)
    @soma_disponivel = Transporte.sum :pares, :conditions => ["deposito_id =? AND status_id != ? AND status_id != ? AND status_id != ?",deposito,4,9,17]
    @soma_disponivel == nil ? 0 : @soma_disponivel
  end
  
  def soma_dia(deposito,data)
    @soma_dia = 0
    @soma_dia = Transporte.sum :pares, :conditions => ["deposito_id =? AND status_id != ? AND status_id != ? AND status_id != ? ",deposito,4,9,17]

    if @soma_dia == nil
      @soma_dia = 0
    end
    
    @soma_dia_fat = Transporte.sum :pares, :conditions => ["ended_at LIKE ? AND deposito_id =? AND status_id = ?","#{data}%",deposito,4]

    if @soma_dia_fat == nil
      @soma_dia_fat = 0
    end
    @soma_dia += @soma_dia_fat
  end
  
  def faturamento_dia_por_hora(status,deposito)
    @transportes = Transporte.find_all_by_status_id(status,:conditions => ["ended_at LIKE ? AND deposito_id =?","%#{Time.now.year}-#{Time.now.month}%",deposito])
  end
  
  def faturamento_dia_por_hora(status,deposito)
    @transportes = Transporte.find_all_by_status_id(status,:conditions => ["ended_at LIKE ? AND deposito_id =?","%#{Time.now.year}-#{Time.now.month}%",deposito])
  end  
  
  def grafico_faturamento_dia_s_d(status, deposito)
    @fat_por_hora = []
    valor = 0
    hoje = Date.new(Time.now.year, Time.now.month, Time.now.day)
    @deposito = Deposito.find(deposito)
    @transportes = @deposito.transportes.find_all_by_status_id(status, :conditions => ["ended_at LIKE ?","%#{hoje.to_s(:db)}%"])
    for i in (0..24)
      @transportes.each do |t|
        if t.ended_at.hour == i
          valor += t.pares
        end
      end
      @fat_por_hora.push valor
      valor = 0
    end
    @fat_por_hora.each {|a| valor += a}
    return valor == 0 ? 0 : @fat_por_hora
  end
  
    
  def faturamento_anterior(deposito,data)
    @valor = 0
    fatant = Fatant.find(:all, :conditions => ["data LIKE ?","#{data}%"])
    fatant.each do |ant|
      @valor += ant.fatantdepos.sum :total, :conditions => ["deposito_id =?",deposito]
    end
    return @valor
  end
  
  def to_label
    self.nome
  end
  
end
